<div id="button-filter-text" style="padding: 5px; margin: 5px 0px;;text-align: center;">
    【本次上传数量为：<b style="color: #00b050" id="total">{$total}</b> 条，重复商机数量：<b style="color: #ff0000" id="repeat">{$repeatNum}</b> 条】
    <a class="remove_repeat" href="javascript:void(0);" style="display: inline-block;padding: 0px 15px; line-height: 30px;font-size: 12px; background: #FF5151;border-radius: 5px; color: #ffffff;">一键清除重复商机</a>
    <a class="startImportCustomer" href="javascript:void(0);" style="display: inline-block;padding: 0px 15px; line-height: 30px;font-size: 12px; background: #FF5151;border-radius: 5px; color: #ffffff;">开始导入表格数据</a>
</div>
<div class="J_tablelist table_list" data-acturi="">
    <table width="100%" cellspacing="0" id="table_list">
        <thead>
        <tr>
            <th align="left">姓名</th>
            <th align="left">手机号</th>
            <th align="left">渠道</th>
            <th align="left">申请金额</th>
            <th align="left">城市</th>
            <th align="left">房</th>
            <th align="left">车</th>
            <th align="left">公积金</th>
            <th align="left">保单</th>
            <th align="left">社保</th>
            <th align="left">营业执照</th>
            <th align="left">备注</th>
            <th align="left">重复？</th>
            <th align="left">状态</th>
            <th align="center">操作</th>
        </tr>
        </thead>
        <tbody>
        <volist name="list" id="val" >
            <tr class="action_tr">
                <td class="name" align="left">{$val.name}</td>
                <td class="mobile" align="left">{$val.mobile}</td>
                <td class="utm_source" align="left">{$val.utm_source}</td>
                <td class="loan_amount" align="left">{$val.loan_amount}</td>
                <td class="city" align="left">{$val.city}</td>
                <td class="has_house" align="left">{$val.has_house}</td>
                <td class="has_car" align="left">{$val.has_car}</td>
                <td class="has_gongjijin" align="left">{$val.has_gongjijin}</td>
                <td class="has_baodan" align="left">{$val.has_baodan}</td>
                <td class="has_shebao" align="left">{$val.has_shebao}</td>
                <td class="has_yingyezhizhao" align="left">{$val.has_yingyezhizhao}</td>
                <td class="remark" align="left">{$val.remark}</td>
                <td class="is_repeat" align="left">{$val.status}</td>
                <td class="action" align="left">待导入</td>
                <td class="oprate" align="center">
                    <a class="remove_tr" href="javascript:void(0);" style="display: inline-block;padding: 0px 10px; line-height: 20px;font-size: 12px; background: #FF5151;border-radius: 5px; color: #ffffff;">删除</a>
                </td>
            </tr>
        </volist>
        </tbody>
    </table>
</div>
<script>
    //删除一行数据
    $('.remove_tr').click(function(){
        let total = $("#total").text();
        $("#total").text(parseInt(total)-1);
        if ($(this).parent().parent().find('.is_repeat').text() == '重复商机') {
            let repeatNum = $("#repeat").text();
            $("#repeat").text(parseInt(repeatNum)-1);
        }
        $(this).parent().parent().remove();
    });
    //处理重复商机数据操作
    $('.remove_repeat').click(function () {
        if($(this).text()=='一键清除重复商机'){
            $(this).text('正在处理中...请等待');
            remove_tr($('#table_list').find('.action_tr:first-child'));
        }else{
            $.yhxia.tip({content:$(this).text(), icon:'alert'});
        }
    });
    /**
     * 移除重复商机数据
     */
    function remove_tr(obj){
        if(obj.text()){
            remove_tr(obj.next('.action_tr'));
            if (obj.find('.is_repeat').text() == '重复商机') {
                let total = $("#total").text();
                let repeatNum = $("#repeat").text();
                $("#total").text(parseInt(total)-1);
                $("#repeat").text(parseInt(repeatNum)-1);
                obj.remove();
            }
        } else {
            $.yhxia.tip({content:'已经结束'});
            $('.remove_repeat').text('已经结束');
            return true;
        }
    }

    var url="{:U('add')}";
    //导入表格数据操作
    $('.startImportCustomer').click(function () {
        if($(this).text()=='开始导入表格数据'){
            $(this).text('正在处理中...请等待');
            process($('#table_list').find('.action_tr:first-child'));
        }else{
            $.yhxia.tip({content:$(this).text(), icon:'alert'});
        }
    });
    /**
     * 执行处理表格数据
     */
    function process(obj){
        if(obj.text()){
            obj.find('.action').text('处理中...');
            let data = {
                name: obj.find('.name').text(),
                mobile: obj.find('.mobile').text(),
                channel: obj.find('.utm_source').text(),
                money: obj.find('.loan_amount').text(),
                city: obj.find('.city').text(),
                has_house: obj.find('.has_house').text(),
                has_car: obj.find('.has_car').text(),
                has_gongjijin: obj.find('.has_gongjijin').text(),
                has_baodan: obj.find('.has_baodan').text(),
                has_shebao: obj.find('.has_shebao').text(),
                has_yingyezhizhao: obj.find('.has_yingyezhizhao').text(),
                remark: obj.find('.remark').text(),
            };
            $.post(url, data, function(result){
                if(result.errNum ===0){
                    obj.find('.action').text('成功');
                }else{
                    obj.find('.action').text(result.errMsg);
                }
                obj.find('.oprate').html('');
                process(obj.next('.action_tr'));
            });
        }else{
            $.yhxia.tip({content:'已经结束'});
            $('.startImportCustomer').text('已经结束');
            return true;
        }
    }
</script>